package com.intelligence.pda.ui.landing;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.intelligence.androidlibrary.utils.SharePreferenceUtils;
import com.intelligence.caixiaoshuangrecyclerview.ProgressStyle;
import com.intelligence.caixiaoshuangrecyclerview.XRecyclerView;
import com.intelligence.pda.R;
import com.intelligence.pda.parameter.LandingMatchParameter;
import com.intelligence.pda.retrofit.ApiCallback;
import com.intelligence.pda.ui.BaseActivity;
import com.intelligence.pda.ui.landing.adapter.LandingMatchAdapter;
import com.intelligence.pda.ui.landing.model.LandingMatchModel;
import com.intelligence.pda.utils.AppConstants;

import java.util.ArrayList;

import butterknife.Bind;

public class LandingMatchActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.ll_toolbar_return)
    LinearLayout ll_toolbar_return;
    @Bind(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    @Bind(R.id.recyclerView)
    XRecyclerView recyclerView;

    private LandingMatchAdapter landingMatchAdapter;
    private ArrayList<LandingMatchModel.DataBean> LandingMatchModel;

    private int page = 1;
    private int pageSize = 10;
    private int dataMaxPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_landing_match);
        tv_toolbar_title.setText("落地配入库");
        initView();
        listener();
    }

    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        recyclerView.setArrowImageView(R.mipmap.iconfont_downgrey);

        View header = LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup) findViewById(android.R.id.content), false);
        TextView tv_title_header = (TextView) header.findViewById(R.id.tv_title_header);
        tv_title_header.setText("到货车次");
        recyclerView.addHeaderView(header);


        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        LandingMatchModel.clear();
                        getlstockList();
                        landingMatchAdapter.notifyDataSetChanged();
                        recyclerView.refreshComplete();
                    }

                }, 1000);
            }

            @Override
            public void onLoadMore() {
                if (page < dataMaxPage) {
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            getlstockList();
                            recyclerView.loadMoreComplete();
                            landingMatchAdapter.notifyDataSetChanged();
                        }
                    }, 1000);
                } else {
                    new Handler().postDelayed(new Runnable() {
                        public void run() {
                            getlstockList();
                            recyclerView.setNoMore(true);
                            landingMatchAdapter.notifyDataSetChanged();
                        }
                    }, 1000);
                }
                page++;
            }
        });

        LandingMatchModel = new ArrayList<>();
        landingMatchAdapter = new LandingMatchAdapter(mActivity, LandingMatchModel);
        recyclerView.setAdapter(landingMatchAdapter);

        LandingMatchModel.clear();
        getlstockList();
        landingMatchAdapter.notifyDataSetChanged();
        recyclerView.refreshComplete();

    }


    private void listener() {
        ll_toolbar_return.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_toolbar_return:
                finish();
                break;
        }

    }

    public void getlstockList() {
        //落地配待入库清单
        LandingMatchParameter landingMatchParameter = new LandingMatchParameter();
        landingMatchParameter.setStart(String.valueOf(page));
        landingMatchParameter.setPageCount(String.valueOf(pageSize));
        landingMatchParameter.setBasicStr(SharePreferenceUtils.getString(AppConstants.BASIC_STR));
        addSubscription(apiStores.getlstockList(landingMatchParameter),
                new ApiCallback<LandingMatchModel>() {
                    @Override
                    public void onSuccess(LandingMatchModel model) {
                        for (int i = 0; i < model.getData().size(); i++) {
                            if (!model.getData().get(i).getOrderNum().equals("0")) {
                                LandingMatchModel.add(model.getData().get(i));
                            }
                        }
                        dataMaxPage = Integer.valueOf(model.getDataMaxPage());
                    }

                    @Override
                    public void onFailure(LandingMatchModel model, String msg) {
                        toastShow(msg);
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);
                    }


                    @Override
                    public void onFinish() {
                    }

                });


    }

    @Override
    protected void onRestart() {
        page = 1;
        LandingMatchModel.clear();
        getlstockList();
        landingMatchAdapter.notifyDataSetChanged();
        recyclerView.refreshComplete();

        super.onRestart();
    }


}
